/*****************************************************************************
 * 
 * Copyright [2013] [Mervin.Wong]
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 *       
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *  
 *****************************************************************************/
package util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import base.Global.NodeIdType;
import base.Global.NumberType;

/**
 *@name FileTool.java
 *@description 文件操作类
 *@author 王进法<Mervin.Wong>
 *@version 
 *@Date 2013-1-14下午8:03:37
 */

public class FileTool {
	private String filePath = null;//读文件路径
	private String fileDirPath = null;
	private String savePath = null;//写文件路径
	
	private BufferedReader reader = null;
	private BufferedWriter writer = null;
	
	public FileTool(){
		
	}
	public FileTool(String filePath){
		File file = new File(filePath);
		if(file.isFile()){
			this.filePath  = filePath;
		}else{
			this.fileDirPath = filePath;
		}
		
	}	
	public FileTool(String filePath, String savePath){
		File file = new File(filePath);
		if(file.isFile()){
			this.filePath  = filePath;
		}else{
			this.fileDirPath = filePath;
		}
		
	}
	
	private void initFileRead(){
		if(this.filePath != null){
			try {
				this.reader =  new BufferedReader(new FileReader(this.filePath));
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	private void initFileWrite(){
		if(this.filePath != null){
			try {
				this.writer = new BufferedWriter(new FileWriter(this.savePath));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	/**
	 * @name statStringInFile
	 * @description  统计文件中的某个字符串出现的次数
	 * @param str
	 * @return
	 */
	public int statStringInFile(String str) {
		// TODO Auto-generated method stub
		String line = new String();
		int num = 0;
		try {
			BufferedReader reader = new BufferedReader(new FileReader(this.filePath));
			try {
				while((line = reader.readLine()) != null){
					if(line.contains(str)){
						num++;
					}
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return num;
	}
	
	/**
	 * @name statFileLines
	 * @description 统计文件中的行数
	 * @return
	 * @throws IOException
	 */
	public int statFileLines(){
		int lineNum = 0;
		BufferedReader reader;
		try {
			reader = new BufferedReader(new FileReader(this.filePath));
			while(reader.readLine() != null){
				++lineNum;
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return lineNum;
	}
	
	/**
	 * @name statNodeNum
	 * @description 统计网络拓扑文件中节点的数量
	 * @param fileName
	 * @param nodeIdType
	 * @return
	 */
	public int statNodeNum(String fileName, NodeIdType nodeIdType){
		HashSet<Number> nodesSet =  new HashSet<Number>();
		String line = null;
		String[] lineArr = null;
		try {
			this.reader = new BufferedReader(new FileReader(fileName));
			while((line = this.reader.readLine()) != null){
				lineArr = line.split("\t");
				for (int i = 0; i < lineArr.length; i++) {
					nodesSet.add(MathTool.str2Number(nodeIdType, lineArr[i]));
				}
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return nodesSet.size();
	}
	/********************************************************************************************************
	 * 
	 * 写文件方法重载
	 * 
	 ********************************************************************************************************/
	/**
	 * @name write
	 * @description 写文件，键值对形式
	 * @param map
	 * @param savePath
	 */
	public void write(HashMap<Number, Number> map, String savePath){
		this.savePath = savePath;
		Set<Number> set = map.keySet();
		StringBuffer buffer= new StringBuffer();
		Object object = null;
		for (Iterator<Number> iterator = set.iterator(); iterator.hasNext();) {
			object = (Number) iterator.next();
			buffer.append(object.toString()).append("\t").append(map.get(object).toString()).append("\r\n");
		}
		try {
			this.writer = new BufferedWriter(new FileWriter(this.savePath));
			this.writer.write(buffer.toString());
			this.writer.flush();
			this.writer.close();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
	/**
	 * @name write
	 * @description 写文件，二维数组形式
	 * @param map
	 * @param savePath
	 */
	public void write(PairList<Number, Number> map, String savePath){
		this.savePath = savePath;
		StringBuffer buffer= new StringBuffer();
		for (int i = 0; i < map.size(); i++) {
			buffer.append(map.getL(i)).append("\t").append(map.getR(i)).append("\r\n");
		}
		try {
			this.writer = new BufferedWriter(new FileWriter(this.savePath));
			this.writer.write(buffer.toString());
			this.writer.flush();
			this.writer.close();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
	/**
	 * @name write
	 * @description 写文件，一维数组形式
	 * @param c
	 * @param savePath
	 */
	public void write(Collection<Number> c, String savePath){
		this.savePath = savePath;
		
		StringBuffer buffer= new StringBuffer();
		Number object = null;
		for (Iterator<Number> iterator = c.iterator(); iterator.hasNext();) {
			object = iterator.next();
			buffer.append(object.toString()).append("\r\n");
		}
		try {
			this.writer = new BufferedWriter(new FileWriter(this.savePath));
			this.writer.write(buffer.toString());
			this.writer.flush();
			this.writer.close();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
	public void write(String str, String savePath){
		this.savePath = savePath;
		try {
			this.writer = new BufferedWriter(new FileWriter(this.savePath));
			this.writer.write(str);
			this.writer.flush();
			this.writer.close();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
	
	/********************************************************************************************************
	 * 
	 * 读文件方法重载
	 * 
	 ********************************************************************************************************/
	/**
	 * @name read2Set
	 * @description 对文件数据到集合中
	 * @param filePath
	 * @param numType
	 * @return
	 */
	public HashSet<Number> read2Set(String filePath,NumberType numType){
		this.filePath = filePath;
		HashSet<Number> set = new HashSet<Number>();
		//this.initFileRead();
		try {
			BufferedReader reader =  new BufferedReader(new FileReader(this.filePath));
			String line = null;
			while ((line = reader.readLine()) != null) {
				set.add(MathTool.str2Num(numType, line));
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return set;
	}
	
	public static void main(String[] args){
		FileTool f = new FileTool("../data/jfk2/201212-jfk-ip.txt");
		D.p(f.statFileLines());
	}
	
}
